import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import SysLayout from "@/components/sys/SysLayout.vue";
import P1 from "@/views/P1.vue";
import P2 from "@/views/P2.vue";
import LoginPage from "@/views/LoginPage.vue";
import P3 from "@/views/P3.vue";

import NProgress from 'nprogress';
import 'nprogress/nprogress.css'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/',
      redirect: '/login'
    },
    {
      path:'/401',
      name: '401',
      component: ()=> import('../views/P401.vue'),
      meta: { title: '401' },

    },
    {
      path:'/403',
      name: '403',
      component: ()=> import('../views/P403.vue'),
      meta: { title: '403' },

    },
    {
      path:'/404',
      name: '404',
      component: ()=> import('../views/P404.vue'),
      meta: { title: '404' },

    },
    {
      path:'/500',
      name: '500',
      component: ()=> import('../views/P500.vue'),
      meta: { title: '500' },

    },
    {
      path: '/login',
      name: 'Login',
      component: LoginPage,
      meta: { title: '登录页' },
    },
    {
      path: '/work',
      name: 'work',
      redirect: '/work/p1',
      component: SysLayout,
      meta: { title: '工作空间' },
      children:[
        {
          path: 'p1',
          name: 'p1',
          component: P1,
          meta: { title: 'personal工作台', requireAuth: false, affix: true, closable: false },
        },
        {
          path: 'p2',
          name: 'p2',
          component: P2,
          meta: { title: '工作台2', requireAuth: true },
        },
        {
          path: 'p3',
          name: 'p3',
          component: ()=> import('../views/P3.vue'),
          meta: { title: 'p3页' },

        },
        {
          path: 'about',
          name: 'about',
          component: () => import('@/views/AboutView.vue'),
          meta: { title: 'about页' },

        }
      ]
    },
    {
      path: '/dir1',
      name: 'dir1',
      redirect: '/dir1/p8',
      component: SysLayout,
      meta: { title: 'dir1空间' },
      children:[
        {
          path: 'p8',
          name: 'p8',
          component: ()=> import('../views/P8.vue'),
          meta: { title: '测试8' },
        },
      ]
    },
    // 404路由必须放在最后
    { path: '/:pathMatch(.*)*', name: 'NotFound', redirect: '/404' },


  ]
});



router.beforeEach((to, from, next) => {
  NProgress.start();
  console.log(from.path);
  console.log(from.name);

  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = 'xx管理系统' +' | '+  to.meta.title;
  }else {
    document.title = 'xx管理系统'.systemTitle;
  }

  next();

  console.log(to.path);
  console.log(to.name);

  // const userStore = useUserStore();

  // if(to.meta.requireAuth) {
  //   next();
  // } else if(to.matched.length == 0) {
  //   next({path: '/error/404'})
  // }  else {
  //   next();
  // }
})

router.afterEach(() => {
  NProgress.done();
})





export default router
